//一般直接写在一个js文件中
layui.use(['element','form'],function () {
    var form = layui.form;
    var buildingId;
    form.on('select(buildingId)', function(data){
         buildingId = data.value;
        if(data.value){
            $.ajax({
                url:"/room/floors",
                data:{buildingId:buildingId},
                dataType:'json',
                success:function(rs){
                    $("#floorNo").empty().append('<option value="">请选择楼层</option>');
                    if(rs.code == 200){
                        $.each(rs.data,function(i,r){
                            $("#floorNo").append('<option value="'+ r +'">'+ r +'</option>');
                        });
                    }
                    form.render('select'); //刷新select选择框渲染
                }
            });
        }
    });

    form.on('select(floorNo)', function(data){
        var floor = data.value;
        if(data.value){
            $.ajax({
                url:"/bed/rooms",
                data:{buildingId:buildingId,floor:floor},
                dataType:'json',
                success:function(rs){
                    $("#roomId").empty().append('<option value=""></option>');
                    if(rs.code == 200){
                        $.each(rs.data,function(i,r){
                            $("#roomId").append('<option value="'+ r.id +'">'+ r.code +'</option>');
                        });
                    }
                    form.render('select'); //刷新select选择框渲染
                }
            });
        }
    });
    //表单验证
    form.verify({
        selectDistinct: function (value, item) { //value：表单的值、item：表单的DOM对象
            var budid = $("#buildingId").val();
            var floor = $("#floorNo").val();
            var roomId = $("#roomId").val();
            var code=$("#code").val();
            var budname = $("#buildingId option:selected").text();
            var roomname = $("#roomId option:selected").text();
            var flag=false;
            $.ajax({
                url:"/bed/selectDistinct",
                dataType:"json",
                async:false,
                type:"post",
                data:{budid:budid,floor:floor,roomId:roomId,code:code},
                success:function(rs){
                    if(rs.code == 200 ){
                        if ( rs.data!=null) flag=true;
                    }
                }
            });
            if (flag) return budname+"-"+floor+"-"+ roomname+ '-'+code+"已存在";
        }
    });
});